<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
<head>
    <title>leoki</title>
    <meta http-equiv="content-type" content="text/html; charset=us-ascii" />
    <meta name="description" content="" />
    <meta name="author" content="" />
    <meta name="keywords" content="" />
    <meta name="generator" content="" />
    <link href="style.css" type="text/css" rel="stylesheet" />
    <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
</head>

<body>

<?php
$_SESSION['logini'] = isset($_SESSION['logini']) ? $_SESSION['logini'] : false;
// read configuration
require ('configuration.html');
// read user-data
require ('user.html');
// read header
require ('header.html');
?>

<!-- Content -->

<div id="content">

<?php 
// information about function and request for login
if ($_SESSION['logini']== false) {
echo "Hier k&ouml;nnen die Vokabeln in einzelne W&ouml;rterboxen sortiert werden. ";
echo "Daf&uuml;r m&uuml;ssen Sie sich anmelden: ";?>
<br /><br />
<?php 
$link = "delvoc.html";
require_once ('loginform.html'); }

else {

// initialize variables
$action = '';
$chosen_words = isset($_REQUEST['chosen_words']) ? $_REQUEST['chosen_words'] : array();

$sql = "SELECT * FROM wordbox0 where boxid = 0 order by timestamp";
$checked = 'wordbox0';
if (isset($_REQUEST['submit0_x'])) { $sql = "SELECT * FROM wordbox0 where boxid = 0 order by timestamp"; $checked = 'wordbox0'; }
if (isset($_REQUEST['submit1_x'])) { $sql = "SELECT * FROM wordbox0 where boxid = 1 order by timestamp"; $checked = 'wordbox1'; }
if (isset($_REQUEST['submit2_x'])) { $sql = "SELECT * FROM wordbox0 where boxid = 2 order by timestamp"; $checked = 'wordbox2'; }
if (isset($_REQUEST['submit3_x'])) { $sql = "SELECT * FROM wordbox0 where boxid = 3 order by timestamp"; $checked = 'wordbox3'; }
if (isset($_REQUEST['submit4_x'])) { $sql = "SELECT * FROM wordbox0 where boxid = 4 order by timestamp"; $checked = 'wordbox4'; }
if (isset($_REQUEST['submit5_x'])) { $sql = "SELECT * FROM wordbox0 where boxid = 5 order by timestamp"; $checked = 'wordbox5'; }
if (isset($_REQUEST['submit6_x'])) { $sql = "SELECT * FROM wordbox0 where boxid = 6 order by timestamp"; $checked = 'wordbox6'; }

if (isset($_REQUEST['delvoc']))   { $action = 'delvoc'; }

// DB Init
$db_link = mysql_connect (MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD);
$db_sel = mysql_select_db( MYSQL_DATABASE ) or die("Error database access");
// DB Query
$db_res = mysql_query( $sql );
if ( ! $db_res ) { die('Error query: ' . mysql_error()); }
                                                
// execute function: show buttons for wordboxes 
switch ( $action ) {
  case 'delvoc' :  delvoc();
  $db_res = mysql_query( $sql );
  if ( ! $db_res ) { die('Error query: ' . mysql_error()); }
  }

show_dialog();

}

// ----------------------- Functions --------------------------------------------------------------------------------------------

function show_dialog() {

  global $checked, $db_res;

  echo '<form action="delvoc.html" method="POST">';

    echo '<h2>Vokabeln entfernen</h2>';
    echo '<div id="block">';
    echo ' <p>Auswahl der Vokabelbox:</p>';
    if ( $checked == 'wordbox0' ) {
    echo '<input type="image" src="images/buttons/lernbox.gif" name="submit0" alt="Lernbox" title=" Lernbox " />';
    } else {
    echo '<input type="image" src="images/buttons/lernboxb.gif" name="submit0" alt="Lernbox" title=" Lernbox " />';
    }
    if ( $checked == 'wordbox1' ) {
    echo '<input type="image" src="images/buttons/box1.gif" name="submit1" alt="Box 1" title=" Box 1 " />';
    } else {
    echo '<input type="image" src="images/buttons/box1b.gif" name="submit1" alt="Box 1" title=" Box 1 " />';
    }
    if ( $checked == 'wordbox2' ) {
    echo '<input type="image" src="images/buttons/box2.gif" name="submit2" alt="Box 2" title=" Box 2 " />';
    } else {
    echo '<input type="image" src="images/buttons/box2b.gif" name="submit2" alt="Box 2" title=" Box 2 " />';
    }
    if ( $checked == 'wordbox3' ) {
    echo '<input type="image" src="images/buttons/box3.gif" name="submit3" alt="Box 3" title=" Box 3 " />';
    } else {
    echo '<input type="image" src="images/buttons/box3b.gif" name="submit3" alt="Box 3" title=" Box 3 " />';
    }
    if ( $checked == 'wordbox4' ) {
    echo '<input type="image" src="images/buttons/box4.gif" name="submit4" alt="Box 4" title=" Box 4 " />';
    } else {
    echo '<input type="image" src="images/buttons/box4b.gif" name="submit4" alt="Box 4" title=" Box 4 " />';
    }
    if ( $checked == 'wordbox5' ) {
    echo '<input type="image" src="images/buttons/box5.gif" name="submit5" alt="Box 5" title=" Box 5 " />';
    } else {
    echo '<input type="image" src="images/buttons/box5b.gif" name="submit5" alt="Box 5" title=" Box 5 " />';
    }
    if ( $checked == 'wordbox6' ) {
    echo '<input type="image" src="images/buttons/box6.gif" name="submit6" alt="Box 6" title=" Box 6 " />';
    } else {
    echo '<input type="image" src="images/buttons/box6b.gif" name="submit6" alt="Box 6" title=" Box 6 " />';
    }
    echo '<br/><br />';

    echo '<table border="1">';
    while ($row = mysql_fetch_array( $db_res, MYSQL_ASSOC)) {

    // Result: String with relevant key-parts
    $key = $row['customerid'] . ',' . $row['boxid'] . ',' . $row['langpairid'] . ',' . $row['langid'] . ',' . $row['1word'] . ',' . $row['2word'] . ',' . $row['3word'] . ',' . $row['word1'] . ',' . $row['word2'] . ',' . $row['word3'] . ',' . $row['timestamp'] ;  

      echo "<tr>";
      echo "<td><input type=Checkbox name=chosen_words[] value=". $key . " /></td>";
      echo "<td> ". $row['customerid'] . " </td>";
      echo "<td> ". $row['boxid'] . " </td>";
      echo "<td> ". $row['langpairid'] . " </td>";
      echo "<td> ". $row['langid'] . " </td>";
      echo "<td> ". $row['1word'] . " </td>";
      echo "<td> ". $row['2word'] . " </td>";
      echo "<td> ". $row['3word'] . " </td>";
      echo "<td> ". $row['word1'] . " </td>";
      echo "<td> ". $row['word2'] . " </td>";
      echo "<td> ". $row['word3'] . " </td>";
      echo "<td> ". $row['timestamp'] . " </td>";
      echo "</tr>";
    }
    echo "</table>";

    echo '<input type="submit" value="Aus Box entfernen" name="delvoc">';
    echo '</div>';
    
  echo '</form>';
  mysql_free_result( $db_res );
}


// change wordbox of chosen words
function delvoc() {
  global $chosen_words, $key ;

	 for( $i=0; $i<count($chosen_words); $i++ ) {
		
		// write parts of checkbox-string in $key-array 
		$str = $chosen_words[$i];

        $key = explode(",",$str); 

        // delete chosen words
		$sql1 = " delete from wordbox0 where ";
        $sql1 .= " customerid = ". $key[0] ." and ";
		$sql1 .= " boxid = ". $key[1] . " and langpairid = ". $key[2] . " and langid = ". $key[3] . " ";
		$sql1 .= " and 1word = '". $key[4] . "' and word1 = '". $key[7] . "' ";

		$db_del = mysql_query( $sql1 );
        if ( ! $db_del )
        { die('Error query: ' . mysql_error()); }
        
        /* echo "<pre>";
        print_r ($sel);
        echo "</pre>"; */                       
        }
} 
?>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />   
</div>

<?php
require ('footer.html');
?>

</body>
</html>